package org.jeecg.modules.question.controller;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.AnswerOnlineController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.question.entity.AoQuestionAnswerRecord;
import org.jeecg.modules.question.service.IAoQuestionAnswerRecordService;
import org.jeecg.modules.question.vo.AoCountVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 答题记录
 *
 * @author thankgod7
 * @version V1.0
 * @date 2022-01-19
 */
@SuppressWarnings("unused")
@Api(tags = "答题记录")
@RestController
@RequestMapping("/question/aoQuestionAnswerRecord")
@Slf4j
public class AoQuestionAnswerRecordController extends AnswerOnlineController<AoQuestionAnswerRecord, IAoQuestionAnswerRecordService> {

    @Resource
    private IAoQuestionAnswerRecordService aoQuestionAnswerRecordService;

    /**
     * 用户答题统计数据查询
     *
     * @return r
     */
    @AutoLog(value = "答题记录-用户答题统计数据查询")
    @ApiOperation(value = "答题记录-用户答题统计数据查询", notes = "答题记录-用户答题统计数据查询")
    @GetMapping(value = "/countUser")
    public Result<?> countUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Integer total = aoQuestionAnswerRecordService.count(new LambdaUpdateWrapper<AoQuestionAnswerRecord>().eq(AoQuestionAnswerRecord::getUsername, sysUser.getUsername()));
        Integer rightCount = aoQuestionAnswerRecordService.count(new LambdaUpdateWrapper<AoQuestionAnswerRecord>().eq(AoQuestionAnswerRecord::getUsername, sysUser.getUsername()).eq(AoQuestionAnswerRecord::getStatus, "0"));
        AoCountVo r = new AoCountVo();
        r.setTotal(total);
        r.setRightCount(rightCount);
        r.setErrorCount(total - rightCount);
        return Result.OK(r);
    }

    /**
     * 分页列表查询
     *
     * @param aoQuestionAnswerRecord param
     * @param pageNo                 pageNo
     * @param pageSize               pageSize
     * @param req                    req
     * @return r
     */
    @AutoLog(value = "答题记录-分页列表查询")
    @ApiOperation(value = "答题记录-分页列表查询", notes = "答题记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AoQuestionAnswerRecord aoQuestionAnswerRecord,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AoQuestionAnswerRecord> queryWrapper = QueryGenerator.initQueryWrapper(aoQuestionAnswerRecord, req.getParameterMap());
        Page<AoQuestionAnswerRecord> page = new Page<>(pageNo, pageSize);
        IPage<AoQuestionAnswerRecord> pageList = aoQuestionAnswerRecordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param aoQuestionAnswerRecord data
     * @return r
     */
    @AutoLog(value = "答题记录-添加")
    @ApiOperation(value = "答题记录-添加", notes = "答题记录-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AoQuestionAnswerRecord aoQuestionAnswerRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String[] corrects = aoQuestionAnswerRecord.getCorrect().split(",");
        String[] selected = aoQuestionAnswerRecord.getSelected().split(",");
        Arrays.sort(corrects);
        Arrays.sort(selected);
        if (Arrays.equals(corrects, selected)) {
            aoQuestionAnswerRecord.setStatus("1");
        } else {
            aoQuestionAnswerRecord.setStatus("0");
        }
        Snowflake snowflake = new Snowflake(1, 1);
        String id = snowflake.nextIdStr();
        aoQuestionAnswerRecord.setId(id);
        if (StringUtils.isEmpty(aoQuestionAnswerRecord.getMainId())) {
            aoQuestionAnswerRecord.setMainId(id);
        }
        aoQuestionAnswerRecord.setUsername(sysUser.getUsername());
        aoQuestionAnswerRecordService.save(aoQuestionAnswerRecord);
        return Result.OK(id);
    }

    /**
     * 编辑
     *
     * @param aoQuestionAnswerRecord data
     * @return r
     */
    @AutoLog(value = "答题记录-编辑")
    @ApiOperation(value = "答题记录-编辑", notes = "答题记录-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AoQuestionAnswerRecord aoQuestionAnswerRecord) {
        aoQuestionAnswerRecordService.updateById(aoQuestionAnswerRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return r
     */
    @AutoLog(value = "答题记录-通过id删除")
    @ApiOperation(value = "答题记录-通过id删除", notes = "答题记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        aoQuestionAnswerRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return r
     */
    @AutoLog(value = "答题记录-批量删除")
    @ApiOperation(value = "答题记录-批量删除", notes = "答题记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.aoQuestionAnswerRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return r
     */
    @AutoLog(value = "答题记录-通过id查询")
    @ApiOperation(value = "答题记录-通过id查询", notes = "答题记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        AoQuestionAnswerRecord aoQuestionAnswerRecord = aoQuestionAnswerRecordService.getById(id);
        if (aoQuestionAnswerRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(aoQuestionAnswerRecord);
    }

    /**
     * 导出excel
     *
     * @param request                r
     * @param aoQuestionAnswerRecord exl
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AoQuestionAnswerRecord aoQuestionAnswerRecord) {
        return super.exportXls(request, aoQuestionAnswerRecord, AoQuestionAnswerRecord.class, "答题记录");
    }

    /**
     * 通过excel导入数据
     *
     * @param request  req
     * @param response res
     * @return r
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AoQuestionAnswerRecord.class);
    }

}
